package com.lzm.ydpt.module.mall.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.basemvp.BasePresenter;
import com.lzm.ydpt.R;
import com.lzm.ydpt.module.mall.fragment.ProductListFragment;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商品列表
 */
public class ProductListActivity extends BaseActivity {
    @BindView(R.id.img_select_price_state)
    ImageView img_select_price_state;
    @BindView(R.id.tv_show_product_title)
    TextView tv_show_product_title;
    @BindView(R.id.tv_shop_price_select)
    TextView tv_shop_price_select;
    @BindView(R.id.tv_shop_buy_select)
    TextView tv_shop_buy_select;
    @BindView(R.id.tv_shop_fiter_select)
    TextView tv_shop_fiter_select;

    private ProductListFragment mListFragment = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mall_product_list;
    }

    @Override
    public BasePresenter initPreData() {
        return null;
    }

    @Override
    public void initView() {
        initTitle();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        mListFragment = ProductListFragment.newInstance(0);
        fragmentTransaction.add(R.id.fragment_product_list, mListFragment);
        fragmentTransaction.commit();

    }

    private void initTitle() {
        tv_show_product_title.setText("商品");

    }


    @OnClick({R.id.img_back_product_list,R.id.ll_shop_peice_select, R.id.tv_shop_buy_select, R.id.tv_shop_fiter_select})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.img_back_product_list:
                finish();
                break;
            case R.id.ll_shop_peice_select:
                if (oldType == 2) {
                    setFilterTitleType(3);
                } else {
                    setFilterTitleType(2);
                }
                break;
            case R.id.tv_shop_buy_select:
                if (oldType != 0) {
                    setFilterTitleType(0);
                }
                break;
            case R.id.tv_shop_fiter_select:
                if (oldType != 1) {
                    setFilterTitleType(1);
                }
                break;
        }
    }

    private int oldType = 2;// 0默认 1销量 2价格

    private void setFilterTitleType(int type) {
        tv_shop_price_select.setSelected(type == 2 || type == 3);
        img_select_price_state.setSelected(type == 2 || type == 3);
        tv_shop_buy_select.setSelected(type == 0);
        tv_shop_fiter_select.setSelected(type == 1);
        if (type == 2 || type == 3) {
            if (type == 2) {
                // 高到底
                img_select_price_state.setImageResource(R.drawable.ic_pop_down);
            } else {
                // 低到高
                img_select_price_state.setImageResource(R.drawable.ic_pop_up);
            }
        } else {
            setFilterStyle(type);
            img_select_price_state.setImageResource(R.drawable.ic_pop_down);
        }
        oldType = type; //type赋值给oldType
        setFilterStyle(oldType);
    }

    /**
     * 筛选样式切换
     */
    private void setFilterStyle(int filterData) {
        mListFragment.setFilter(filterData);
    }
}